//
//  NSObject+Expand.m
//  Pension
//
//  Created by gavin on 2020/5/20.
//  Copyright © 2020 XLH. All rights reserved.
//

#import "NSObject+MJUI.h"
#import <objc/message.h>

@implementation NSObject (MJUI)

+ (NSString*)mj_className {
    
    //OC
    return [NSStringFromClass(self) componentsSeparatedByString:@"."].lastObject;
    //swift 获取类名字(swift 中有小数点区分)
    
}

- (NSString*)mj_className {
    return [self.class mj_className];
}

///Model转化为字典
- (NSMutableDictionary *)mj_toDic {
    NSObject *object = self;
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    unsigned int count;
    objc_property_t *propertyList = class_copyPropertyList([object class], &count);
    
    for (int i = 0; i < count; i++) {
        objc_property_t property = propertyList[i];
        const char *cName = property_getName(property);
        NSString *name = [NSString stringWithUTF8String:cName];
        NSObject *value = [object valueForKey:name];//valueForKey返回的数字和字符串都是对象
        
        if ([value isKindOfClass:[NSString class]] || [value isKindOfClass:[NSNumber class]]) {
            //string , bool, int ,NSinteger
            [dic setObject:value forKey:name];
            
        } else if ([value isKindOfClass:[NSArray class]] || [value isKindOfClass:[NSDictionary class]]) {
            //字典或字典
            [dic setObject:[((NSArray*)value) mj_toArrayOrDic] forKey:name]; // toArrayOrDic

        } else if (value == nil) {
            //null
            [dic setObject:[NSNull null] forKey:name];//这行可以注释掉?????
            
        } else {
            //model
            [dic setObject:[value mj_toDic] forKey:name]; // toDic
        }
    }
    
    //return [dic copy];
    return [dic mutableCopy];
}

///将可能存在Model数组转化为普通数组
- (id)mj_toArrayOrDic {
    id origin = self;
    if ([origin isKindOfClass:[NSArray class]]) {
        //数组
        NSMutableArray *array = [NSMutableArray array];
        for (NSObject *object in origin) {
            if ([object isKindOfClass:[NSString class]] || [object isKindOfClass:[NSNumber class]]) {
                //string , bool, int ,NSinteger
                [array addObject:object];
 
            } else if ([object isKindOfClass:[NSArray class]] || [object isKindOfClass:[NSDictionary class]]) {
                //数组或字典
                [array addObject:[(NSArray *)object mj_toArrayOrDic]]; // toArrayOrDic
 
            } else {
                //model
                [array addObject:[object mj_toDic]]; // toDic
            }
        }
 
        //return [array copy];
        return [array mutableCopy];
 
    } else if ([origin isKindOfClass:[NSDictionary class]]) {
        //字典
        NSDictionary *originDic = (NSDictionary *)origin;
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        for (NSString *key in originDic.allKeys) {
            id object = [originDic objectForKey:key];
 
            if ([object isKindOfClass:[NSString class]] || [object isKindOfClass:[NSNumber class]]) {
                //string , bool, int ,NSinteger
                [dic setObject:object forKey:key];
 
            } else if ([object isKindOfClass:[NSArray class]] || [object isKindOfClass:[NSDictionary class]]) {
                //数组或字典
                [dic setObject:[object mj_toArrayOrDic] forKey:key]; // toArrayOrDic
 
            } else {
                //model
                [dic setObject:[object mj_toDic] forKey:key]; // toDic
            }
        }
 
        //return [dic copy];
        [dic mutableCopy];
    }
 
    return [NSNull null];
}

@end
